package com.example.androidnote;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.Nullable;

import com.example.androidnote.Util.DBUtils;
import com.example.androidnote.Util.NoteDB;

public class DetailActivity extends Activity implements View.OnClickListener {

    private Button delete,back;
    private ImageView img;
    private VideoView vid;
    private TextView tv;
    private SQLiteDatabase dbwriter;
    int i_id;
    String s_img,s_vid,text;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_layout);
        delete = findViewById(R.id.d_delete);
        delete.setOnClickListener(this);
        back = findViewById(R.id.d_back);
        back.setOnClickListener(this);
        img = findViewById(R.id.d_img);
        vid = findViewById(R.id.d_ved);
        tv = findViewById(R.id.d_tv);
        dbwriter = new NoteDB(this).getWritableDatabase();
        i_id = getIntent().getIntExtra(NoteDB.ID,0);
        s_img = getIntent().getStringExtra(NoteDB.IMG_PATH);
        s_vid = getIntent().getStringExtra(NoteDB.VED_PATH);
        text = getIntent().getStringExtra(NoteDB.CONTENT);
        if (!s_img.equals("error")){
            img.setVisibility(View.VISIBLE);
            img.setImageBitmap(BitmapFactory.decodeFile(s_img));
        }
        if (!s_vid.equals("error")){
            vid.setVisibility(View.VISIBLE);
            vid.setVideoURI(Uri.parse(s_vid));
            vid.start();
        }
        tv.setText(getIntent().getStringExtra(NoteDB.CONTENT));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.d_delete:{
                dbwriter.delete(NoteDB.TABLE,NoteDB.ID+"="+i_id,null);
                finish();
                break;
            }
            case R.id.d_back:{
                finish();
                break;
            }
        }
    }
}
